package cn.rxyb.desgin.mediator;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author YJH
 * @version 1.0
 * @description 具体的中介者
 * @date 2020/7/3 15:11
 */
public class ConcreteMediator extends AbstractMediator {

    private List<AbstractColleague> colleagues = new ArrayList<>();

    @Override
    public void register(AbstractColleague colleague) {

        // 查看是否已经包含该元素信息
        if (!colleagues.contains(colleague)) {
            colleagues.add(colleague);
            colleague.setAbstractMediator(this);
        }
    }

    @Override
    public void relay(AbstractColleague colleague) {
        colleagues.stream().filter(e -> !e.equals(colleague)).collect(Collectors.toList()).forEach(e -> e.receive());
    }
}
